//
//  OrderCell.m
//  Ufan
//
//  Created by yangqingluo on 15/12/11.
//  Copyright © 2015年 yangqingluo. All rights reserved.
//

#import "OrderCell.h"
#import "UIImageView+EMWebCache.h"
#import "UIResponder+Router.h"

@interface OrderCell()

@property (strong, nonatomic) UIView *bottomLineView1;
@property (strong, nonatomic) UILabel *timeLabel;
@property (strong, nonatomic) UILabel *itemNameLabel;
@property (strong, nonatomic) UILabel *typeLabel;
@property (strong, nonatomic) UILabel *totalLabel;
@property (strong, nonatomic) UIButton *payButton;
@property (strong, nonatomic) UIButton *sureButton;

@end

@implementation OrderCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.textLabel.font = [UIFont boldSystemFontOfSize:12.0];
        
        self.detailTextLabel.font = [UIFont systemFontOfSize:12.0];
        self.detailTextLabel.textColor = self.textLabel.textColor;
        self.detailTextLabel.textAlignment = NSTextAlignmentRight;
        
        _bottomLineView1 = [[UIView alloc] init];
        _bottomLineView1.backgroundColor = UFCOLOR_GRAYLINE;
        [self.contentView addSubview:_bottomLineView1];
        
        _timeLabel = [[UILabel alloc]init];
        _timeLabel.textColor = [UIColor blackColor];
        _timeLabel.font = [UIFont systemFontOfSize:12.0];
        [self.contentView addSubview:_timeLabel];
        
        _itemNameLabel = [UILabel new];
        _itemNameLabel.textColor = [UIColor lightGrayColor];
        _itemNameLabel.font = [UIFont systemFontOfSize:12.0];
        [self.contentView addSubview:_itemNameLabel];
        
        _typeLabel = [UILabel new];
        _typeLabel.textColor = _itemNameLabel.textColor;
        _typeLabel.font = _itemNameLabel.font;
        [self.contentView addSubview:_typeLabel];
        
        _totalLabel = [UILabel new];
        _totalLabel.textColor = self.detailTextLabel.textColor;
        _totalLabel.font = self.detailTextLabel.font;
        _totalLabel.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:_totalLabel];
        
        _payButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _payButton.frame = CGRectMake(0, 0, 100, 36);
        _payButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        [_payButton setBackgroundColor:[UIColor orangeColor]];
        _payButton.layer.cornerRadius = 5;
        _payButton.layer.masksToBounds = YES;
        [_payButton setTitle:@"立即支付" forState:UIControlStateNormal];
        [_payButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_payButton addTarget:self action:@selector(payNow) forControlEvents:UIControlEventTouchUpInside];
        _payButton.hidden = YES;
        [self.contentView addSubview:_payButton];
        
        _sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _sureButton.frame = CGRectMake(0, 0, 100, 36);
        _sureButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        [_sureButton setBackgroundColor:[UIColor blackColor]];
        _sureButton.layer.cornerRadius = 5;
        _sureButton.layer.masksToBounds = YES;
        [_sureButton setTitle:@"确认收货" forState:UIControlStateNormal];
        [_sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sureButton addTarget:self action:@selector(sureNow) forControlEvents:UIControlEventTouchUpInside];
        _sureButton.hidden = YES;
        [self.contentView addSubview:_sureButton];

    }
    return self;
}

- (void)payNow{
    [self routerEventWithName:UF_Router_Event_OrderPay userInfo:@{@"order":self.order}];
}

- (void)sureNow{
    [self routerEventWithName:UF_Router_Event_OrderSure userInfo:@{@"order":self.order}];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    _bottomLineView1.frame = CGRectMake(15, self.contentView.height - 51, self.contentView.width - 30, 1);
    
    
    self.timeLabel.frame = CGRectMake(_bottomLineView1.left, 10, _bottomLineView1.width, 20);
    self.timeLabel.text = [NSString stringWithFormat:@"下单时间：%@",stringFromDate([NSDate dateWithTimeIntervalSince1970:self.order.createDate])];
    
    self.imageView.frame = CGRectMake(_bottomLineView1.left, self.timeLabel.bottom + 20, 60, 45);
    self.detailTextLabel.frame = CGRectMake(_bottomLineView1.right - 80, self.imageView.top, 80, 15);
    self.textLabel.frame = CGRectMake(self.imageView.right + 5, self.imageView.top, self.detailTextLabel.left - self.imageView.right - 5, self.detailTextLabel.height);
    self.itemNameLabel.frame = CGRectMake(self.textLabel.left, self.textLabel.bottom, self.textLabel.width, self.textLabel.height);
    self.typeLabel.frame = CGRectMake(self.itemNameLabel.left, self.itemNameLabel.bottom, self.itemNameLabel.width, self.itemNameLabel.height);
    
    if (self.order.orderType == OrderTypeNotPay) {
        _payButton.hidden = NO;
        _sureButton.hidden = YES;
        _payButton.center = CGPointMake(_bottomLineView1.right - 0.5 * _payButton.width, self.contentView.height - 25);
        self.totalLabel.frame = CGRectMake(_bottomLineView1.left, _bottomLineView1.bottom, _payButton.left - _bottomLineView1.left - 10, self.height - _bottomLineView1.bottom);
    }
    else if (self.order.orderType == OrderTypeSended) {
        _payButton.hidden = YES;
        _sureButton.hidden = NO;
        _sureButton.center = CGPointMake(_bottomLineView1.right - 0.5 * _sureButton.width, self.contentView.height - 25);
        self.totalLabel.frame = CGRectMake(_bottomLineView1.left, _bottomLineView1.bottom, _sureButton.left - _bottomLineView1.left - 10, self.height - _bottomLineView1.bottom);
    }
    else{
        _payButton.hidden = YES;
        _sureButton.hidden = YES;
        self.totalLabel.frame = CGRectMake(_bottomLineView1.left, _bottomLineView1.bottom, _bottomLineView1.width, self.height - _bottomLineView1.bottom);
    }
    
    
    if (self.order.productItems.count) {
        UFProductItem *item = self.order.productItems[0];
        
        NSURL *imageUrl = [NSURL URLWithString:item.mainUrl];
        [self.imageView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@""]];
        
        self.detailTextLabel.text = [NSString stringWithFormat:@"%.2f元 × %d",item.itemPrice,item.quantity];
        self.textLabel.text = item.productName;
        self.itemNameLabel.text = [NSString stringWithFormat:@"类型:%@",item.itemName.length ? item.itemName : @"无"];
    }
    
    self.typeLabel.text = stringFromOrderType(self.order.orderType);
    self.typeLabel.textColor = colorFromOrderType(self.order.orderType);
    self.totalLabel.text = [NSString stringWithFormat:@"合计：%.2f元",self.order.totalPrice];
    
}


+(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 180;
}

@end
